<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>提货券新增</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${jspath}/jquery/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>

<script type="text/javascript" src="${jspath}/jquery/jquery.alerts.js"></script>

<script type="text/javascript">
	var row_count = 0;
	$(function() {
		//新增行
		$("#insertRow")
				.click(
						function() {
							var table = $("#data_tabs");
							var row = $("<tr></tr>");
							var td0 = $("<td></td>");
							var td1 = $("<td></td>");
							var td2 = $("<td></td>");
							var td3 = $("<td></td>");
							var td4 = $("<td style='border-top: 1px solid #fff;'></td>");
							td0
									.append($("<input class='form-control input-sm' id='itemName"+row_count+"' type='text' />"));
							row.append(td0);

							td1
									.append($("<input class='form-control input-sm text-right' id='quantity"
											+ row_count
											+ "' type='text' onchange='getPrepayAmount(this.id)'/>"));
							row.append(td1);

							td2
									.append($("<input class='form-control input-sm text-right' id='unitPrice"
											+ row_count
											+ "' type='text' onchange='getPrepayAmount(this.id)'/>"));
							row.append(td2);

							td3
									.append($("<input class='form-control input-sm text-right' id='prepayAmount"
											+ row_count
											+ "' type='text' readonly/>"));
							row.append(td3);

							td4
									.append($("<button type='button' id='delete"
											+ row_count
											+ "' class='btn btn-warning btn-sm' onclick='deleteRow(this.id)' style='margin-left: 10px;'>删除</button>  "));
							row.append(td4);

							table.append(row);
							row_count++;
							changePrepayAmount();
						});

		function changePrepayAmount() {
			if (row_count >= 1) {
				for ( var k = 0; k < row_count; k++) {
					if ($("#prepayAmount" + k).val() == null
							|| $("#prepayAmount" + k).val() == "") {
						$("#prepayAmount" + k).val("0");
						$("#unitPrice" + k).val("0");
					}
					if ($("#quantity" + k).val() == null
							|| $("#quantity" + k).val() == "") {
						$("#quantity" + k).val("1");
					}
					if ($("#unitPrice" + k).val() == null
							|| $("#unitPrice" + k).val() == "") {
						$("#unitPrice" + k).val("0");
					}
				}
			}
		}

		$("#defineTicketForm").validation({
			icon : true
		});

		//新增提货券信息
		$("#saveBtn")
				.click(
						function() {
							if ($("#defineTicketForm").valid(this, '内容出错') == false) {
								return false;
							}
							if (validateInline() == false) {
								jAlert('填写信息不完整', '提示');
								return false;
							}

							if (check1() == false) {
								$("#msgInfo").text("填写信息不完整");
								return false;
							}
							var ticketName = $("#ticketName").val();
							var verificationMethod = $("#verificationMethod")
									.val();
							var itemName = $("#itemName").val();
							var prepayAmount = $("#prepayAmount").val();
							var quantity = $("#quantity").val();

							var radio = document.getElementsByName("ifPrepay");
							var ifPrepay = null;
							for ( var i = 0; i < radio.length; i++) {
								if (radio[i].checked == true) {
									ifPrepay = radio[i].value;
									break;
								}
							}

							var dataStr = itemName + "@#" + prepayAmount + "@#"
									+ quantity + "###";
							if (row_count >= 1) {
								for ( var k = 0; k < row_count; k++) {
									var amount = $("#prepayAmount" + k).val();
									if (amount != undefined && amount != null
											&& amount != "") {
										var itemNames = $("#itemName" + k)
												.val();
										var prepayAmounts = $(
												"#prepayAmount" + k).val();
										var quantitys = $("#quantity" + k)
												.val();

										dataStr = dataStr + itemNames + "@#"
												+ prepayAmounts + "@#"
												+ quantitys + "###";
									}
								}
							}
							$
									.ajax({
										url : '${webcontext}/crmdp/dealer/coupon/saveRedemption',
										type : 'post',
										data : {
											'ticketName' : ticketName,
											'verificationMethod' : verificationMethod,
											'ifPrepay' : ifPrepay,
											'deductionMethod':$("#deductionMethod").val(),
											'deductionAmount':$("#deductionAmount").val(),
											'dataStr' : dataStr
										},
										success : function(data) {
											if (data.code == true) {
												jAlert(data.msg,"提示",function(){
													window.location = '${webcontext}/crmdp/dealer/coupon/toDefineTicKet';
												});
											} else if (data.code == false) {
												jAlert(data.msg,"提示");
											}
										},
										error : function() {
											jAlert("服务器错误，请联系管理员","提示");
										},
									});

						});

		//返回
		$("#returnBtn")
				.click(
						function() {
							window.location.href = "${webcontext}/crmdp/dealer/coupon/toDefineTicKet";
						});

	});

	//计算所有的预计金额
	function sumPrepayAmount() {
		var sumPrepayAmount = $("#prepayAmount").val();
		if (row_count >= 1) {
			for ( var k = 0; k < row_count; k++) {
				var amount = $("#prepayAmount" + k).val();
				if (amount != undefined && amount != null && amount != "") {
					var prepayAmounts = $("#prepayAmount" + k).val();
					sumPrepayAmount = fmoney(parseFloat(sumPrepayAmount)
							+ parseFloat(prepayAmounts), 1);
					$("#prepayAmount" + k)
							.val(
									fmoney(parseFloat($("#prepayAmount" + k)
											.val()), 1));
					$("#unitPrice" + k).val(
							fmoney(parseFloat($("#unitPrice" + k).val()), 1));
				}
			}
		}
		$("#prepayAmountTask").val(fmoney(parseFloat(sumPrepayAmount), 1));
		$("#prepayAmount").val(fmoney(parseFloat($("#prepayAmount").val()), 1));
		$("#unitPrice").val(fmoney(parseFloat($("#unitPrice").val()), 1));
	}

	//计算每一笔预计金额
	function getPrepayAmount(id) {
		var prepayAmount = "";
		if (row_count >= 1) {
			var num = id.substring(id.length - 1, id.length);
			if (!isNaN(num)) {
				var unitPrice = $("#unitPrice" + num).val();
				if (unitPrice.indexOf(',') > 0) {
					unitPrice = unitPrice.replace(',', '');
				}
				var quantity = $("#quantity" + num).val();
				if (isNaN(unitPrice)) {
					$("#msgInfo").text("单价只能为数字");
					return false;
				} else {
					$("#msgInfo").text("");
				}
				if (isNaN(quantity)) {
					$("#msgInfo").text("数量只能为数字");
					return false;
				} else {
					$("#msgInfo").text("");
				}
				if (unitPrice != null && unitPrice != "" && quantity != null
						&& quantity != "") {
					prepayAmount = parseFloat(unitPrice) * parseFloat(quantity);
					$("#prepayAmount" + num).val(
							fmoney(parseFloat(prepayAmount), 1));
					sumPrepayAmount();
				}
			} else {
				if (check1() == false) {
					return false;
				}
				var unitPrice = $("#unitPrice").val();
				if (unitPrice.indexOf(',') > 0) {
					unitPrice = unitPrice.replace(',', '');
				}
				var quantity = $("#quantity").val();
				if (unitPrice != null && unitPrice != "" && quantity != null
						&& quantity != "") {
					prepayAmount = parseFloat(unitPrice)
							* parseFloat($("#quantity").val());
					$("#prepayAmount").val(fmoney(parseFloat(prepayAmount), 1));
					sumPrepayAmount();
				}
			}
		} else {
			if (check1() == false) {
				return false;
			}
			var unitPrice = $("#unitPrice").val();
			if (unitPrice.indexOf(',') > 0) {
				unitPrice = unitPrice.replace(',', '');
			}
			var quantity = $("#quantity").val();
			if (unitPrice != null && unitPrice != "" && quantity != null
					&& quantity != "") {
				prepayAmount = parseFloat(unitPrice) * parseFloat(quantity);
				$("#prepayAmount").val(fmoney(parseFloat(prepayAmount), 1));
				sumPrepayAmount();
			}
		}
	}

	function check1() {
		var unitPrice = $("#unitPrice").val();
		if (unitPrice.indexOf(',') > 0) {
			unitPrice = unitPrice.replace(',', '');
		}
		var quantity = $("#quantity").val();
		if (isNaN(unitPrice)) {
			$("#msgInfo").text("");
			$("#msgInfo").text("单价只能为数字");
			return false;
		} else {
			$("#msgInfo").text("");
		}
		if (isNaN(quantity)) {
			$("#msgInfo").text("");
			$("#msgInfo").text("数量只能为数字");
			return false;
		} else {
			$("#msgInfo").text("");
		}
		return true;
	}

	//删除行
	function deleteRow(id) {
		$("#" + id).parent().parent().remove();
		sumPrepayAmount();
	}

	//格式化金额，保留两位小数点
	function fmoney(s, n) {
		n = n > 0 && n <= 20 ? n : 2;
		s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
		var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
		t = "";
		for ( var i = 0; i < l.length; i++) {
			//t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
			t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "" : "");
		}
		return t.split("").reverse().join("") + "." + r;
	}

	//校验提货券的详细信息是否填写
	function validateInline() {
		if (row_count >= 1) {
			for ( var k = 0; k < row_count; k++) {
				var itemName = $("#itemName" + k).val();
				var unitPrice = $("#unitPrice" + k).val();
				var prepayAmount = $("#prepayAmount" + k).val();
				var quantity = $("#quantity" + k).val();
				if (itemName != undefined && itemName != null && itemName != "") {
					if (unitPrice == null || unitPrice == "") {
						return false;
					} else {
						if (!/^[0-9]{1}\d*(\.\d{1,2})?$/.test(unitPrice)) {
							$("#msgInfo").text("");
							$("#msgInfo").text("单价只能为数字");
							return false;
						}
					}
					if (prepayAmount == null || prepayAmount == "") {
						return false;
					} else {
						if (!/^[0-9]{1}\d*(\.\d{1,2})?$/.test(prepayAmount)) {
							$("#msgInfo").text("");
							$("#msgInfo").text("预付金额只能为数字");
							return false;
						}
					}
					if (quantity == null || quantity == "") {
						return false;
					} else {
						if (!/^[1-9]{1}\d*(\.\d{1,2})?$/.test(quantity)) {
							$("#msgInfo").text("");
							$("#msgInfo").text("数量只能为数字");
							return false;
						}
					}
					return true;
				}else{
					if (itemName == null || itemName == "") {
						return false;
					}
				}
			}
		} else {
			var itemName = $("#itemName").val();
			var unitPrice = $("#unitPrice").val();
			var prepayAmount = $("#prepayAmount").val();
			var quantity = $("#quantity").val();
			if (itemName == null || itemName == "") {
				return false;
			}
			if (unitPrice == null || unitPrice == "") {
				return false;
			}
			if (prepayAmount == null || prepayAmount == "") {
				return false;
			}
			if (quantity == null || quantity == "") {
				return false;
			}
			if (check1() == false) {
				return false;
			}
			return true;
		}
	}
</script>
</head>
<style>
.hr {
	text-align: center;
	width: 100%;
	background-color: #0071C1;
	height: 1px;
	margin-bottom: 10px;
}
.rowDiv {
	width: 90%;
}

.formDiv {
	float: left;
	width: 50%;
}

</style>
<body>
	<div id="content">
		<div style="color: #025D7B; height: 26px; line-height: 26px;">
			<span>优惠券定义</span> <span>&gt;</span> <span>提货券新增</span>
		</div>
		<div class="hr"></div>
		<form:form id="defineTicketForm" action="" class="form-horizontal"
			method="post" modelAttribute="dto" role="form">
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="ticketName">名称</label>
					<div class="col-sm-6">
						<input class="form-control input-sm" id="ticketName" type="text"
							check-type="required" required-message="请填写名称" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="deductionMethod">抵扣方式</label>
					<div class="col-sm-6">
						<select id="deductionMethod" class="form-control input-sm">
							<option value="AMOUNT">金额</option>
							<option value="PERCENT">百分比</option>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="deductionAmount">抵扣值</label>
					<div class="col-sm-6">
						<input class="form-control input-sm text-right"
							id="deductionAmount" type="text" check-type="number" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="verificationMethod">抵扣凭证</label>
					<div class="col-sm-6">
						<select id="verificationMethod" class="form-control input-sm">
							<option value="CODE">抵扣码</option>
							<option value="N/A">无</option>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="prepayAmountTask">预付金额</label>
					<div class="col-sm-6">
						<input class="form-control input-sm text-right"
							id="prepayAmountTask" type="text" readonly value="0" />
					</div>
				</div>
			</div>
			<div class="row hidden">
				<label class="col-sm-2 control-label" for="ifPrepay">是否预付费</label> <label
					class="radio-inline"> <input type="radio" name="ifPrepay"
					id="ifPrepay1" value="1">是
				</label> <label class="radio-inline"> <input type="radio"
					name="ifPrepay" id="ifPrepay2" value="0">否
				</label>
			</div>
			<table id="data_tabs" class="table"
				style="margin-top: 20px; margin-left: 20px; width: 98%;">
				<thead>
					<tr>
						<th class="text-center">产品说明</th>
						<th class="text-center">数量</th>
						<th class="text-center">单价</th>
						<th class="text-center">预付金额</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td><input class="form-control input-sm" id="itemName"
							type="text" /></td>
						<td><input class="form-control input-sm text-right"
							id="quantity" type="text" value="1"
							onchange="getPrepayAmount(this.id);" /></td>
						<td><input class="form-control input-sm text-right"
							id="unitPrice" type="text" value="0"
							onchange="getPrepayAmount(this.id);" /></td>
						<td><input class="form-control input-sm text-right"
							id="prepayAmount" type="text" value="0" readonly /></td>
						<td style='border-top: 1px solid #fff;'></td>
					</tr>
				</tbody>
			</table>
			<div style="margin-left: 300px;">
				<button type="button" class="btn btn-success btn-sm" id="saveBtn">
					<i class="glyphicon glyphicon-floppy-saved" style="color: #FFFFFF"></i>&nbsp;保
					存
				</button>
				<button type="button" class="btn btn-success btn-sm" id="insertRow">
					<i class="glyphicon glyphicon-plus" style="color: #FFFFFF"></i>&nbsp;新增产品
				</button>
				<button type="button" class="btn btn-success btn-sm" id="returnBtn">
					<i class="glyphicon glyphicon-retweet" style="color: #FFFFFF"></i>&nbsp;返回
				</button>
				<span id="msgInfo" class="text-danger"></span>
			</div>
		</form:form>
	</div>
</body>
</html>